package org.example.admin.service;

import lombok.extern.slf4j.Slf4j;
import org.example.admin.exception.exceptions.KaptchaExpiredException;
import org.example.admin.exception.exceptions.KaptchaVerifyException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class AdminKaptchaService {

    RedisTemplate<String, Object>  redisTemplate;

    public AdminKaptchaService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void verifyKaptcha(String code, String kaptchaId){

        String rightCode = (String) redisTemplate.opsForValue().get("kaptchaKey:" + kaptchaId);
        redisTemplate.delete("kaptchaKey:" + kaptchaId);
        if(rightCode == null){
            throw new KaptchaExpiredException("验证码超时");
        }
        if(!rightCode.equals(code)){
            throw new KaptchaVerifyException("验证码错误");

        }

    }
}
